import defUtil from "./utils/defUtil.js";
import {eventEmitter} from "./model/EventEmitter.js";
import goodsSale from "./page/goodsSale.js";

/**
 * 静态路由
 * @param url {string}
 * @param title {string}
 */
const staticRoute = (url = window.location.href, title = document.title) => {
    const parseUrl = defUtil.parseUrl(url);
    console.log('静态路由:', url, parseUrl)
    if (url.includes('ksjhaoka.com/#/login')) {
        eventEmitter.send('登录选项卡显隐', true)
        eventEmitter.send('切换标签页', '登录')
        return
    }
    if (goodsSale.isGoodsSalePage(url)) {
        goodsSale.installTopTabsListener()
    }

}

/**
 * 动态路由
 */
const dynamicRouting = () => {
    let oldUrl = window.location.href;
    setInterval(() => {
        const newUrl = window.location.href;
        if (oldUrl === newUrl) return;
        oldUrl = newUrl;
        const title = document.title;
        callback(newUrl, title)
    }, 1000);

    const callback = (url, title) => {
        const parseUrl = defUtil.parseUrl(url);
        console.log('动态路由:', parseUrl, url, title);
        if (!url.includes('ksjhaoka.com/#/login')) {
            eventEmitter.send('登录选项卡显隐', false)
        }
        if (goodsSale.isGoodsSalePage()) {
            eventEmitter.send('切换标签页', '产品管理')
            goodsSale.installTopTabsListener()
        }
    }
}


export default {
    staticRoute,
    dynamicRouting
}
